package kuzmenkov.oip.adapters.byu.jigl;

import jigl.image.Image;
import jigl.image.ROI;
import jigl.image.io.ImageInputStreamJAI;
import jigl.image.io.ImageOutputStreamJAI;
import jigl.image.ops.GaussianNoise;
import kuzmenkov.oip.adapters.ImageProcessingAdapter;
import kuzmenkov.oip.common.FileTool;

import java.awt.geom.Rectangle2D;
import java.util.logging.Logger;

/**
 * This class adapts a gaussian noise generator module to use it by using
 * a web-interface
 * User: Michael Kuzmenkov
 * Date: 17.04.2009
 * Time: 23:17:40
 */
public class GaussianNoiseGeneratorAdapter extends ImageProcessingAdapter{
    private Logger log = Logger.getLogger(HistogramEqualizerAdapter.class.getName());

    /**
     * Adds gaussion noise distributions to an image.
     * It takes three parameters that are a path to a source image (image),
     * a standard deviation of Gaussian noise (float) and a region of interest of a source image (selection).
     * It returns the path to the new image (image).
     * @param params the input parameters
     * @return the output parameters
     * @throws Exception
     */
    public Object[] process(Object[] params) throws Exception {
        String srcPath = (String) params[0];
        Float stdDev = (Float) params[1];
        Rectangle2D selection = (Rectangle2D) params[2];
        log.info("srcPath="+srcPath+", stdDev="+stdDev+", selection="+selection);

        FileTool ftool = FileTool.getInstance();
        srcPath = ftool.relPathToAbs(srcPath);

        ImageInputStreamJAI inputStream = new ImageInputStreamJAI(srcPath);
        Image srcImage = inputStream.read();

        ROI roi = new ROI((int)selection.getMinX(),(int)selection.getMinY(),
                (int) selection.getMaxX(),(int)selection.getMaxY());

        GaussianNoise gaussNoise = new GaussianNoise(stdDev);
        Image dstImage = gaussNoise.apply(srcImage,roi);


        String dstFileName = "1.png";
        String absDstPath = ftool.getTempDir(getSessionId())+dstFileName;
        log.info("destination path="+absDstPath);
        ImageOutputStreamJAI outputStream = new ImageOutputStreamJAI(absDstPath);
        outputStream.writePNG(dstImage);

        String relDstPath = ftool.getRelativeTempDir(getSessionId())+dstFileName;
        log.info("relative destination path="+relDstPath);

        return new Object[]{relDstPath};
    }
}